import { mapState } from 'vuex'
import { userModule } from '@/store/modules/user'

import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { subsidyServices } from '@/common/services/subsidy'
import { TableMixin } from '@/mixin'
import { Modal } from 'ant-design-vue'
import { payServices } from '@/common/services/pay'
import { publishServices } from '@/common/services/publish'
import PublicMemberList from '../../subsidy/publish/PublicMemberList.vue'

@Component({
  components: {
    PublicMemberList
  }
})
export class PublishMixin extends Mixins(TableMixin) {
  @Prop({
    type: String,
  })
  public currentAreaId!: string

  get isLarge() {
    return userModule.isLarge
  }

  get isPlant() {
    return userModule.isPlant
  }

  // @ts-ignore
  public waitExportColumns: ITable.IColumn[] = [
    // {
    //   title: '公示编号',
    //   dataIndex: 'pub_id',
    //   key: 'pub_id'
    // },

    {
      title: '发放批次',
      dataIndex: 'batch_id',
      key: 'batch_id',
      width: 200,
      form: {
        type: 'a-select',
        placeholder: '请输入耕地总面积',
        renderMode: ['farmland'],
        isShowQuery: true,
        isShowList: false,
        isShowForm: false,
        dict: [],
      },
    },

    {
      title: '发放批次号',
      dataIndex: 'commit_id',
      key: 'commit_id',
    },
    {
      title: this.isLarge ? '大户编号' : '农户编号',
      dataIndex: 'member_id',
      key: 'member_id',
      form: {
        type: 'a-input',
        placeholder: this.isLarge ? '大户编号' : '农户编号',
        isShowQuery: true,
        isShowFrom: false,
      },
    },
    {
      title: this.isLarge ? '大户名称' : '户主姓名',
      dataIndex: 'member_nam',
      key: 'member_nam',
      form: {
        type: 'a-input',
        placeholder: this.isLarge ? '大户名称' : '户主姓名',
        isShowQuery: true,
        isShowFrom: false,
      },
    },
    {
      title: this.isLarge ? '身份证号' : '户主身份证',
      dataIndex: 'member_sfz',
      key: 'member_sfz',
    },
    {
      title: '行政区',
      dataIndex: 's_a_nam_all',
      key: 's_a_nam_all',
    },
    !this.isPlant
      ? {
          title: '补贴参照',
          width: 110,
          key: 'pay_unit',
          align: 'center',
          scopedSlots: { customRender: 'pay_unit' },
          form: {
            type: 'a-select',
            placeholder: '请选择补贴核算依据',
            isShowFrom: false,
            isExport: true,
          },
        }
      : {},
    {
      title: '补贴面积',
      width: 110,
      dataIndex: 'bt_size',
      align: 'right',
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      key: 'bt_size',
      form: {
        type: 'a-input',
        placeholder: '请输入补贴面积',
        isShowQuery: true,
        isShowFrom: false,
        querySection: true,
      },
    },
    {
      // title: '补贴标准(亩/元)',
      title: '补贴标准(元/亩)',
      // slots: { title: 'pay_price' },
      width: 80,
      dataIndex: 'pay_price',
      align: 'right',
      key: 'pay_price',
    },
    {
      title: '补贴金额',
      key: 'pay_price_total',
      dataIndex: 'pay_price_total',
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      align: 'right',
      width: 120,
    },
    {
      title: '收款人信息',
      children: [
        {
          title: '收款人姓名',
          key: 'bk_user',
          dataIndex: 'bk_user',
          width: 120,
          form: {
            type: 'a-input',
            placeholder: '请输入收款人姓名(农户填身份证/机构填社会信用代码)',
          },
        },
        {
          title: '收款人身份证',
          key: 'bk_bid',
          dataIndex: 'bk_bid',
          width: 170,
          form: {
            type: 'a-input',
            placeholder: '请输入收款人身份证',
          },
        },
        {
          title: '收款人账号',
          key: 'bk_num',
          dataIndex: 'bk_num',
          width: 120,
          form: {
            type: 'a-input',
            placeholder: '请输入收款人账号',
          },
        },
        {
          title: '收款人开户行',
          key: 'bk_nam',
          dataIndex: 'bk_nam',
          width: 120,
          form: {
            type: 'a-input',
            placeholder: '请输入收款人开户行',
          },
        },
        {
          title: '备注',
          key: 'readme',
          dataIndex: 'readme',
          width: 120,
          form: {
            type: 'a-textarea',
            placeholder: '请输入备注',
          },
        },
      ],
    },

    {
      title: '补贴户信息查重',
      dataIndex: 'member_filter',
      key: 'member_filter',
      width: 260,
      form: {
        type: 'a-checkbox-group',
        placeholder: '请选择查重/查错条件',
        dict: [
          {
            label: '身份证查重',
            value: 'member_sfz_repeat',
          },
          {
            label: '银行卡号查重',
            value: 'member_bank_num_repeat',
          },
          {
            label: '姓名查重',
            value: 'member_nam_repeat',
          },
        ],
        mode: 'multiple',
        renderMode: ['filter'],
        isShowAll: false,
        isShowFrom: false,
        isShowList: false,
        isShowQuery: true,
      },
    },

    {
      title: '银行兑付信息',
      children: [
        {
          title: '兑付银行',
          key: 'bk_op',
          dataIndex: 'bk_op',
          width: 120,
        },
        {
          title: '兑付时间',
          key: 'bk_time',
          dataIndex: 'bk_time',
          width: 120,
        },
        {
          title: '银行备注',
          key: 'bk_read',
          dataIndex: 'bk_read',
          width: 120,
        },
        {
          title: '兑付状态',
          key: 'bk_price',
          dataIndex: 'bk_price',
          width: 120,
          form: {
            type: 'a-radio-group',
            isShowQuery: true,
            isShowFrom: false,
            dict: [
              {
                label: '待提交',
                value: '0',
              },
              {
                label: '已提交',
                value: '1',
              },
              {
                label: '兑付失败',
                value: '2',
              },
              {
                label: '兑付成功',
                value: '3',
              },
            ],
          },
        },
      ],
    },
    {
      title: '操作',
      scopedSlots: { customRender: 'action' },
      fixed: 'right',
    },
  ].filter((_) => _.title)

  public handlerExportExecl(isSupportSort = false) {
    const temp = this.tableApi.exportExecl
    this.tableApi.exportExecl = payServices.getExportList
    const fields = this.waitExportColumns
      .filter(
        (column: ITable.IColumn) =>
          !column.form ||
          (typeof column.form.isExport !== 'undefined'
            ? column.form.isExport
            : typeof column.form.isShowList === 'undefined' ||
              column.form.isShowList)
      )
      .map((column) => {
        if (column.key !== 'field') {
          return column.children || column
        }
        return column
      })
      .flat()
      .map((column) => {
        // action 操作列表
        if (!column.key || column.key === 'action') return null
        return {
          label: (column.title || column.titleAlias) as string,
          value: column.key as string,
          disabled: this.requireExportFields.includes(column.key),
          selected: this.requireExportFields.includes(column.key),
        }
      })
      .filter((_) => _)
      // @ts-ignore
      .sort((a, b) => b.disabled - a.disabled)
    this.tableExportExecl(
      {},
      {
        // @ts-ignore
        fields,
      },
      {
        isSupportSort,
      }
    ).finally(() => {
      this.tableApi.exportExecl = temp;
    })
  }

  // 起草公示
  public handlerPublic(isLargeMember: boolean = false) {
    this.$dialog(PublicMemberList, {
      currentAreaId: this.currentAreaId,
      isPreview: true,
      isLargeMember
    }, {
      title: '待起草公示人员预览表',
      width: '80%',
      centered: true,
      maskClosable: false,
      okText: '确认发布',
      cancelText: '放弃',
      on: {
        ok: async () => {
          await publishServices.updatePublish({
            s_a_id: this.currentAreaId,
            is_big_member: isLargeMember
          })
          this.$message.success('起草公示成功')
          this.getData()
          return true;
        }
      }
    })
  }
}
